package com.lxk.controller;

import com.lxk.pojo.Cashout;
import com.lxk.pojo.WxUser;
import com.lxk.service.CashoutService;
import com.lxk.service.UserService;
import com.lxk.vo.CashoutModel;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.CodeMsg;
import com.lxk.vo.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @author JohnSoybean
 * @create 2020/4/3 19:20
 * @description 提现
 */
@Controller
public class CashoutController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CashoutService cs;
    @Autowired
    private UserService us;

    /**
     * 提现列表展示
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @return
     */
    @GetMapping("/cashout")
    public String toCashout(HttpServletRequest req, Model model,
                                Integer pageIndex, Integer num, Integer totalPages,
                                Integer sort, Integer order ) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 10;
        }
        if (null == totalPages) {
            totalPages = cs.getCashoutPages(num);
        }
        PageModel<CashoutModel> cashouts = cs.selCashoutPage(pageIndex, num, totalPages, sort, order);
        model.addAttribute("data", cashouts);
        return "cashout";
    }


    /**
     * 通过提现
     * @param cashoutId
     * @return
     */
    @GetMapping("/cashout/agree")
    @ResponseBody
    public Result<Integer> agreeCashout(Integer cashoutId) {
        Cashout cashout = new Cashout();
        cashout.setCashoutId(cashoutId);
        cashout.setHandleTime(new Date());
        cashout.setIsFinished(1);
        cs.updCashout(cashout);
        return Result.success(1);
    }

    /**
     * 拒绝提现
     * @param cashoutId
     * @return
     */
    @GetMapping("/cashout/reject")
    @ResponseBody
    public Result<Integer> rejectCashout(Integer cashoutId) {
        Cashout cashout = cs.selCashout(cashoutId);
        if (cashout == null) {
            return Result.error(CodeMsg.CASHOUT_NOT_EXIST);
        }
        cashout.setCashoutId(cashoutId);
        cashout.setHandleTime(new Date());
        cashout.setIsFinished(2);
        cs.updCashout(cashout);
        //返还账户预扣款金额
        BigDecimal cashoutPrice = cashout.getCashoutPrice();
        Integer wxUserId = cashout.getWxUserId();
        WxUser wxUser = us.selUserById(wxUserId);
        BigDecimal account = wxUser.getAccount();
        account = account.add(cashoutPrice);
        wxUser.setAccount(account);
        us.updUser(wxUser);
        return Result.success(1);
    }

}
